package cn.openea.perpartner.link.web.controller;

import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.link.web.entity.ShareLinkConfig;
import cn.openea.perpartner.link.web.entity.dto.ShareLinkConfigDTOFactory;
import cn.openea.perpartner.link.web.entity.vo.AddShareLinkConfigVo;
import cn.openea.perpartner.link.web.entity.vo.ShareLinkSearchVo;
import cn.openea.perpartner.link.web.service.IShareLinkConfigService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @author ysk
 * @email zane_ysk@qq.com
 * @date 2023-05-21 23:45:18
 */
@Api(tags = "分享链接配置")
@RestController
@RequestMapping("/share/link")
public class ShareLinkConfigController extends BaseController {

    @Resource
    private IShareLinkConfigService shareLinkConfigService;
    @Resource
    private ShareLinkConfigDTOFactory shareLinkConfigDTOFactory;

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    public TableDataInfo list(ShareLinkSearchVo shareLinkSearchVo) {
        startPage();
        List<ShareLinkConfig> shareLinkConfigs = shareLinkConfigService.findListBySearchVo(shareLinkSearchVo);
        TableDataInfo dataTable = getDataTable(shareLinkConfigs);
        dataTable.setRows(shareLinkConfigs.stream()
                .map(item -> shareLinkConfigDTOFactory.toInfoDTO.apply(item))
                .collect(Collectors.toList()));
        return dataTable;
    }


    /**
     * 获取详情
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        if (id == null) {
            return AjaxResult.error("id不能为空");
        }
        ShareLinkConfig shareLinkConfig = shareLinkConfigService.getById(id);
        return AjaxResult.success(shareLinkConfigDTOFactory.toInfoDTO.apply(shareLinkConfig));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody @Valid AddShareLinkConfigVo addVo) {
        shareLinkConfigService.addShareLinkConfig(addVo);
        return AjaxResult.success();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody @Valid AddShareLinkConfigVo addVo) {
        ShareLinkConfig oldShareLinkConfig = Optional.ofNullable(shareLinkConfigService.getById(addVo.getId()))
                .orElseThrow(() -> new ServiceException("数据不存在"));
        shareLinkConfigService.updateShareLinkConfig(addVo, oldShareLinkConfig);
        return AjaxResult.success();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody String[] keys) {
        for (String key : keys) {
            shareLinkConfigService.deleteByKey(key);
        }
        return AjaxResult.success();
    }

}
